#!C:\Python26
# -*- coding: UTF-8 -*-

import cookiesupport

import sys
import string
import urllib
import urllib2
import os.path
import os

if __name__ == "__main__":
	COOKIEFILE = 'cookies.lwp'				#Handle Cookies
	# the path and filename to save your cookies in
	
	cj = None
	ClientCookie = None
	cookielib = None
	
	# Let's see if cookielib is available
	try:
		import cookielib
	except ImportError:
		# If importing cookielib fails
		# let's try ClientCookie
		try:
			import ClientCookie
		except ImportError:
			# ClientCookie isn't available either
			print "ClientCookie is not available"
			urlopen = urllib2.urlopen
			Request = urllib2.Request
		else:
			# imported ClientCookie
			print "ClientCookie is available"
			urlopen = ClientCookie.urlopen
			Request = ClientCookie.Request
			cj = ClientCookie.LWPCookieJar()
	
	else:
		# importing cookielib worked
		urlopen = urllib2.urlopen
		Request = urllib2.Request
		cj = cookielib.LWPCookieJar()
		# This is a subclass of FileCookieJar
		# that has useful load and save methods
	
	if cj is not None:
	# we successfully imported
	# one of the two cookie handling modules
	
		if os.path.isfile(COOKIEFILE):
			# if we have a cookie file already saved
			# then load the cookies into the Cookie Jar
			cj.load(COOKIEFILE)
	
		# Now we need to get our Cookie Jar
		# installed in the opener;
		# for fetching URLs
		if cookielib is not None:
			# if we use cookielib
			# then we get the HTTPCookieProcessor
			# and install the opener in urllib2
			opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
			urllib2.install_opener(opener)
	
		else:
			# if we use ClientCookie
			# then we get the HTTPCookieProcessor
			# and install the opener in ClientCookie
			ClientCookie.install_opener(opener)
		
		
	user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
	values = {'name' : 'Name',
		  'location' : 'Somewhere',
		  'language' : 'Klingon' }
	headers = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
	
	data = urllib.urlencode(values)

if __name__ != "__main__":

	
	print "Note: getcookie cannot tell if you logged in correctly. Make sure you username and pass are correct."

def getcookie(mode, uname, password):
	         
	
	if mode == "DA":
		print uname
		print password

		
		params = urllib.urlencode({"username" : uname, "password" : password, "reusetoken" : "1", "ref" : "https://www.deviantart.com/users/loggedin", "action" : "login"})
		f = opener.open('https://www.deviantart.com/users/login', params)
		fh = open("cookie.html", "w")
		fh.write(f.read())
		f.close()
		fh.close()



		if cj is not None:
			cj.save(COOKIEFILE)					 # save the cookies again
			print "Have these cookies: "
			for cookie in cj:
				print "Cookie: ",
				print cookie
			
			
	if mode == "FA":
		
		logondict = {"retard_protection":"1", "action":"login", "name":uname, "pass":password}
		
		params = urllib.urlencode(logondict)
		f = opener.open('http://www.furaffinity.net/login/', params)
		datastr = f.read()
		datalist = datastr.split("\n")
		f.close()
		if cj is not None:
			cj.save(COOKIEFILE)					 # save the cookies again
			print "Have these cookies: "
			for cookie in cj:
				print "Cookie: ",
				print cookie
		
if __name__ == "__main__":
		
	if len(sys.argv) < 3:
		print "\n\nInsufficent arguements\n"
		cookiesupport.syntaxerror()
		sys.exit(0)
	
	
	if string.lower(sys.argv[1]) == "fa":
		mode = "FA"
		print "Mode = "+mode
	elif string.lower(sys.argv[1]) == "da":
		mode = "DA"
		print "Mode = "+mode
	else:
		print "\n\nMode error\n"
		cookiesupport.syntaxerror()
		sys.exit(0)
		
	getcookie(mode, sys.argv[2], sys.argv[3])
		


